package cn.edu.ctbu.datajpa01.webapi;

import cn.edu.ctbu.datajpa01.core.PageUtils;
import cn.edu.ctbu.datajpa01.domain.Student;
import cn.edu.ctbu.datajpa01.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.TypeCache;
import org.aspectj.apache.bcel.classfile.Module;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.sql.Struct;
import java.util.List;


@RestController
@RequestMapping("/webapi/student")
@Slf4j
public class StudentRestController {
    @Autowired
    private StudentService studentService;

    /**
     * 读取的url：/webapi/student/list
     * @return
     */
    @GetMapping("/list")
    public List<Student> getAll(){
        List<Student> students=studentService.findAll();
        return students;
    }


    /***
     * 分页读取全部内容
     * @return
     */
    @GetMapping("/getbypage")
    public  PageUtils getByPage(@RequestParam(value = "page",defaultValue = "0") Integer page,
                                @RequestParam(value = "size",defaultValue = "10") Integer size,
                                @RequestParam(value = "name",defaultValue = "") String name,
                                @RequestParam(value = "no",defaultValue="")String no){
//        System.out.println(page);
//        System.out.println(size);
        Sort sort=Sort.by(Sort.Direction.DESC,"id");

        Page<Student> studentPage;
        if(StringUtils.isEmpty(no)) {
            Pageable pageable = PageRequest.of(page, size, sort);
            studentPage = studentService.findAll(pageable);
        }else {
            Student student =new Student();
            student.setName(no);
            Pageable pageable =PageRequest.of(0,10,sort);

            ExampleMatcher matcher =ExampleMatcher.matching()
                    .withMatcher("no",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Student> example = Example.of(student,matcher);

            studentPage =studentService.findAll(example,pageable);
        }



            if(StringUtils.isEmpty(name)){
            Pageable pageable =PageRequest.of(page,size,sort);
            studentPage =studentService.findAll(pageable);

        }else {
            Student student =new Student();
            student.setName(name);
            Pageable pageable =PageRequest.of(0,10,sort);

            ExampleMatcher matcher =ExampleMatcher.matching()
                    .withMatcher("name",ExampleMatcher.GenericPropertyMatchers.contains());

            Example<Student> example = Example.of(student,matcher);

            studentPage =studentService.findAll(example,pageable);
        }

        PageUtils pageUtils=new PageUtils(studentPage.getContent(),studentPage.getTotalElements());
        return pageUtils;

    }

    /**
     * /webapi/student/get/id
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Student get(@PathVariable Integer id){
         Student student=studentService.getById(id);

         student.setPassword("");
         return student;
    }
//根据学号来查询学生
    @GetMapping("/get1/{no}")
    public Student get(@PathVariable String no){
        Student student=studentService.getByNo(no);

        student.setPassword("");
        return student;
    }
//根据姓名来查询学生
    @GetMapping("/find/{name}")
    public List<Student> find(@PathVariable String name){
        List<Student> students=studentService.findByName(name);

        return students;

    }





    @PostMapping("/insert")
    public Student insert(Student student){
        Student student1=studentService.Insert(student);
        return student1;
    }



    @PutMapping("/update")
    public Student update(Student student){

        //读取旧的数据
        Student oldstudent=studentService.getById(student.getId());

        if(StringUtils.isEmpty(student.getPassword())){
            //没有新密码，那么就是旧密码
            student.setPassword(oldstudent.getPassword());
        }

        student=studentService.Update(student);


        return student;
}

    /***
     * 删除信息
     * @param id
     */
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable Integer id){
        studentService.Delete(id);
        log.info(id.toString());
    }



}
